<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        .adBox {
            width: 300px;
            height: 400px;
            background-color: #6ff;
            position: fixed;
            right: 5px;
            bottom: 5px;
            display: none;

        }

        .title {
            position: relative;
            font-size: 14px;
        }

        .title .tips {
            text-indent: 5px;
        }

        .title .close {
            position: absolute;
            right: 0;
            top: 0;
            padding: 0 4px;
            background-color: #f00;
        }

        .body img {
            width: 100%;
        }
    </style>
</head>

<body>
    <div class="adBox">
        <div class="title">
            <div class="tips">距离广告关闭还有0秒</div>
            <div class="close">x</div>
        </div>
        <div class="body">
            <a href="">
                <img src="https://img20.360buyimg.com/babel/s580x740_jfs/t1/169223/11/9002/236742/603f38feEa4a8d83e/4ace79eafcac855c.jpg!cc_290x370.webp"
                    alt="">
            </a>
        </div>
    </div>
</body>
<script>
    // 小广告
    // (1)  延迟5秒 显示小广告
    // (2)  小广告显示之后 倒计时10秒   => 关闭
    // (3)  重复(1) (2) 操作
    // (4)  点击关闭按钮  => 暂停小广告(清除延时器)


    var adBox = document.getElementsByClassName("adBox")[0];
    var tips = document.getElementsByClassName("tips")[0];
    var closeDiv = document.getElementsByClassName("close")[0];
    var delayTimer = null;
    var timer = null;

    loop();


    closeDiv.onclick = function(){
        // 问题2   延时器中嵌套的有计时器  只清除延时器 对计时器没有影响 => 计时器还会正常允许 调用loop
        // 解决方法: 清除延时器 和 计时器
        adBox.style.display = "none";
        clearTimeout(delayTimer);
        clearInterval(timer);
        loop();
    }

    function loop() {
        clearTimeout(delayTimer); // 每次开启下一个之前 清除上一个
        delayTimer =  setTimeout(function () {

            adBox.style.display = "block";
            var num = 10;
            tips.innerText = `距离广告关闭还有${num}秒`;
            timer = setInterval(function () {
                num--;
                tips.innerText = `距离广告关闭还有${num}秒`;

                if (num == 0) {
                    clearInterval(timer);
                    adBox.style.display = "none";
                    loop();
                }

            }, 1000);
         
        }, 5000);
    }


    // 问题!!!!!!!!!
    // 计时器 一般情况下 不会嵌套计时器  => 每隔计时器都是独立的 不受其他因素干扰 (外层的计时器 不会等待内层计时器结束)
    // 解决方法 => 延时器改计时器   => 延时器和函数递归  


   /*  setInterval(function () {  //每隔5秒执行

        adBox.style.display = "block";
        var num = 10;
        tips.innerText = `距离广告关闭还有${num}秒`;
        var timer = setInterval(function () {   // 10后面关闭
            num--;
            tips.innerText = `距离广告关闭还有${num}秒`;

            if (num == 0) {
                clearInterval(timer);
                adBox.style.display = "none";
            }

        }, 1000);

    }, 5000); */



</script>

</html>